<?php
function require_dir($dir) {
	if (file_exists ( $dir )) {
		$files = scandir ( $dir );
		for($i = 2; $i < count ( $files ); $i ++) {
			$reg = '/\\' . DIRECTORY_SEPARATOR . '$/';
			if (preg_match ( $reg, $dir )) {
				$file = $dir . $files [$i];
			} else {
				$file = $dir . DIRECTORY_SEPARATOR . $files [$i];
			}
			if (is_file ( $file )) {
				if (preg_match ( '/.+(.php)$/', $file )) {
					require_once ($file);
				}
			} else if (is_dir ( $file )) {
				require_dir ( $file );
			}
		}
	}
}
//返回省略主域名和具体文件请求的基本url  例如：www.xx.com/system/index.php/.../../  返回 /system/
function baseUrl() {
	$request = $_SERVER ['PHP_SELF'];
	$baseurl = substr ( $request, 0, strpos ( $request, basename ( $_SERVER ['SCRIPT_NAME'] ) ) + strlen ( basename ( $_SERVER ['SCRIPT_NAME'] ) ) );
	return substr ( $baseurl, 0, strlen ( $baseurl ) - 9 );
}
//返回具体action的路径 以http://开头
function urlToAction($controller, $action) {
	$url = "http://";
	$url .= $_SERVER ['HTTP_HOST'];
	$url .= baseUrl () . "index.php/";
	$url .= $controller . "/" . $action;
	return $url;
}

//重定向
function redirectTo($url) {
	Header ( "HTTP/1.1 303 See Other" );
	Header ( "Location:" . $url );
}

function redirectToAction($controller, $action) {
	redirectTo ( urlToAction ( $controller, $action ) );
}

function rootUrl(){
	$url="http://";
	$base = baseUrl();
	return $url.$_SERVER['HTTP_HOST'].$base;
}

function islogin(){
	if(!isset($_SESSION['userinfo']['type']) || $_SESSION['userinfo']['type']==UserBean::$_GUEST)
		return false;
	return true;
}

function getJobTitles(){
	return Sysconfig::$jobtitles;
}
function getJobTitle($key){
	if(isset(Sysconfig::$jobtitles[$key]))
		return Sysconfig::$jobtitles[$key];
	return "unknown";
}
function getTeacherTypes(){
	return Sysconfig::$teacherTypes;
}
function getTeacherType($key){
	if(isset(Sysconfig::$teacherTypes[$key]))
		return Sysconfig::$teacherTypes[$key];
	return "unknown";
}
function getInstitutes(){
	return Sysconfig::$institutes;
}
function getInstitute($name){
	if(isset(Sysconfig::$institutes[$name]))
		return Sysconfig::$institutes[$name];
	return "unknown";
}
function getUserType(){
	return $_SESSION["userinfo"]["type"];
}
function isAdmin(){
	return getUserType()==Userbean::$_ADMIN;
}
function isGuest(){
	return getUserType()==Userbean::$_GUEST;
}
function isSenate(){
	return getUserType()==Userbean::$_SENATE;
}
function isInstitue(){
	return getUserType()==Userbean::$_INSTITUE;
}

function getAjaxResult($result,$title,$content,$type = "general"){
	$json=array(
			"result"=>$result,
			"title"=>urlencode($title),
			"content"=>urlencode($content),
			"type"=>$type
	);
	return urldecode(json_encode($json));
}
//设置回滚
function setRollback($title,$deepth=0,$paras=array(),$controller="",$action=""){
	if($controller==""||$action==""){
		$controller=$_GET['controller'];
		$action=$_GET['action'];
	}
	$rollback = array ();
	if (isset ( $_SESSION ['rollback'] ) && $deepth > 0) {
		for($i = 0; $i < $deepth; $i ++)
			$rollback [$i] = $_SESSION ['rollback'] [$i];
	}
	$rollback [$deepth] = array ("title"=>$title, "controller"=>$controller, "action"=>$action ,"paras"=>$paras);
	$_SESSION ['rollback'] = $rollback;
}
function getRollback(){
	return $_SESSION['rollback']; 
}

function getActivityTypes(){
	return Sysconfig::$activityType;
}
function getActivityType($key){
	if(isset(Sysconfig::$activityType[$key]))
		return Sysconfig::$activityType[$key];
	return "unknown";
}
function getactivitySubTypes(){
	return Sysconfig::$activitySubType;
}
function getActivitySubType($key){
	$types = Sysconfig::$activitySubType;
	foreach ($types as $g =>$t)
		foreach ($t as $k => $value){
		if($k == $key)
			return $value;
	}
	return "unknown";
}
?>